import { defineStore } from 'pinia'
import {
  woundedListPage, //伤票列表分页
  woundedFind, //查询伤票及相关信息
} from '@/utils/api/woundedfz/woundedfz'
export const useWoundedfzStore = defineStore(
  'woundedfz',
  () => {
    //伤票列表
    const List: AnyArray = []
    const woundedList = reactive({ List })
    const pageNum = ref(1) // 当前页数
    const islower = ref(false) // 是否到底部
    const TagStatus = ref('') // 标签状态
    //搜索列表
    const isSearchPage = ref(false) // 是否是搜索页
    const searchParam = ref('')
    const searchList: any = []
    const searchListData = reactive({ searchList })
    // 获取伤票列表
    const getWoundedList = async () => {
      if (islower.value) {
        return
      }
      if (isSearchPage.value) {
        if (searchParam.value == '') {
          searchListData.searchList = []
          pageNum.value = 1
        }
      }
      islower.value = true
      const res: any = await woundedListPage({
        pageNum: pageNum.value,
        pageSize: 5,
        TagStatus: TagStatus.value || '',
        searchParam: searchParam.value,
      })
      let Obj: any = res.data
      if (pageNum.value <= Obj.totalPage) {
        pageNum.value++
        setTimeout(() => {
          if (isSearchPage) {
            searchListData.searchList.push(...Obj.list)
          }
          woundedList.List.push(...Obj.list)
        }, 200)
      } else {
        uni.showToast({
          title: '没有更多数据了',
          icon: 'none',
          duration: 1500,
        })
      }
      islower.value = false
    }
    const clearDate = () => {
      woundedList.List = []
      searchListData.searchList = []
      searchParam.value = ''
      isSearchPage.value = false
      pageNum.value = 1
      islower.value = false
    }
    // 查询伤票及相关信息
    const findWounded = async (TagCode: string) => {
      const res: any = await woundedFind({ TagCode })
      return res.data
    }
    return {
      islower, // 是否到底部
      woundedList, // 伤票列表
      pageNum, // 当前页数
      getWoundedList, // 获取伤票列表
      TagStatus, // 状态
      clearDate, // 清空数据
      searchListData, // 搜索列表
      isSearchPage, // 是否是搜索页
      searchParam, // 搜索参数
      findWounded, // 查询伤票及相关信息
    }
  },
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
      paths: ['TagStatus'],
    },
  },
)
